#ifndef B_PHYSICS_DEBUG_DRAWER_H_
#define B_PHYSICS_DEBUG_DRAWER_H_

#include "LinearMath/btIDebugDraw.h"
#include <d3dx9.h>
#include <vector>
#include "Vec3.h"

/* Special debug declaration, this ensures the class will work in any project */
struct DebugVert
{
    DebugVert(){}	
    void Set(float posX, float posY, float posZ,
               float colR, float colG, float colB){pos = D3DXVECTOR4(posX,posY,posZ,1);
                                                   color = D3DXVECTOR4(colR, colG, colB, 1);}
    D3DXVECTOR4 pos;
    D3DXVECTOR4 color;

    static IDirect3DVertexDeclaration9* s_dcl;
    static void RegisterDeclarations();
};

/* Defines the functions bullet needs in order to draw it's debug data. 
   Uses a dynamic vertex buffer for D3D rendering. */
class LineDrawer : public btIDebugDraw
{
public:
    void Initialize();
    void Release();
    //must do this AT LEAST
    virtual void    drawLine(const btVector3& from,const btVector3& to,const btVector3& color);
    void    drawLine(const D3DXVECTOR3& from,const D3DXVECTOR3& to,const D3DXVECTOR3& color);
    void    drawLine(const Vec3& from,const Vec3& to,const Vec3& color);
    void    drawLine( float fromX,float fromY,float fromZ, float toX,float toY,float toZ, float colR,float colG,float colB );
    //error handling
    virtual void	reportErrorWarning(const char* warningString);
    //these appear to be optional
    virtual void	drawContactPoint(const btVector3& PointOnB,const btVector3& normalOnB,btScalar distance,int lifeTime,const btVector3& color){}
    virtual void	draw3dText(const btVector3& location,const char* textString){}

    //these are trivial, I don't know why the base class doesn't implement this?
    virtual void	setDebugMode(int debugMode){ m_debugMode = debugMode; }
    virtual int		getDebugMode() const {return m_debugMode;}

    //
    void DrawAllLines();
    void DrawPermanentLine(const btVector3& from,const btVector3& to,const btVector3& color);
    void ClearTempLines(){m_tTos.clear();m_tFroms.clear();m_tColors.clear();}
    void ClearAllLines(){m_pTos.clear();m_tFroms.clear();m_tColors.clear();ClearTempLines();}
private:
    LPDIRECT3DVERTEXBUFFER9 m_points;
    int m_debugMode;
    //temporary requests
    std::vector<D3DXVECTOR3> m_tFroms;
    std::vector<D3DXVECTOR3> m_tTos;
    std::vector<D3DXVECTOR3> m_tColors;
    //permanent
    std::vector<D3DXVECTOR3> m_pFroms;
    std::vector<D3DXVECTOR3> m_pTos;
    std::vector<D3DXVECTOR3> m_pColors;
};
#endif